package cn.net.dckj.modules.biz.common;

import java.util.Calendar;
import java.util.Date;

/**
 * 教育类业务工具类
 *
 * @author hqj
 * @version 2019-04-16
 */
public class EducationUtils {

    /**
     * 获取给定时间学年
     *
     * @param date 给定时间
     * @return
     */
    public static int getSchoolYear(Date date) {
        int schoolYear = 0;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        // 月份从8月开始，进入下一学年，否则属于上一学年
        if (month > 7) {
            schoolYear = calendar.get(Calendar.YEAR);
        } else {
            schoolYear = calendar.get(Calendar.YEAR) - 1;
        }
        return schoolYear;
    }

    /**
     * 获取当前时间学年
     *
     * @return
     */
    public static int getCurrentSchoolYear() {
        Calendar now = Calendar.getInstance();
        Date date = now.getTime();
        return getSchoolYear(date);
    }

    /**
     * 获取给定时间学期
     *
     * @param date 给定时间
     * @return
     */
    public static int getTeam(Date date) {
        int team = 0;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        // 月份从2到7月属于第二学期，否则属于第一学期
        if (month > 1 && month < 8) {
            team = 2;
        } else {
            team = 1;
        }
        return team;
    }

    /**
     * 获取当前时间学期
     *
     * @return
     */
    public static int getCurrentTeam() {
        Calendar now = Calendar.getInstance();
        Date date = now.getTime();
        return getTeam(date);
    }

    /**
     * 获取给定时间学周
     *
     * @param date           给定时间
     * @param startDate      学期开始时间
     * @param firstDayOfWeek 一周开始（从周几开始）
     * @return
     */
    public static int getSchoolWeek(Date date, Date startDate, int firstDayOfWeek) {
        int schoolWeek = 0;
        Calendar start = Calendar.getInstance();
        start.setFirstDayOfWeek(firstDayOfWeek);
        start.setTime(startDate);
        int y1 = start.get(Calendar.YEAR);
        int w1 = start.get(Calendar.WEEK_OF_YEAR);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(firstDayOfWeek);
        int y2 = calendar.get(Calendar.YEAR);
        int w2 = calendar.get(Calendar.WEEK_OF_YEAR);
        if ((y2 == y1) && (w2 > w1)) {
            schoolWeek = w2 - w1 + 1;
        } else if (y2 == (y1 + 1)) {
            schoolWeek = 53 - w1 + w2;
        }
        return schoolWeek;
    }

    /**
     * 获取给定时间学周
     *
     * @param date      给定时间
     * @param startDate 学期开始时间
     * @return
     */
    public static int getSchoolWeek(Date date, Date startDate) {
        return getSchoolWeek(date, startDate, Calendar.MONDAY);
    }

    /**
     * 获取当前时间学周
     *
     * @param startDate      学期开始时间
     * @param firstDayOfWeek 一周开始（从周几开始）
     * @return
     */
    public static int getCurrentSchoolWeek(Date startDate, int firstDayOfWeek) {
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        return getSchoolWeek(date, startDate, firstDayOfWeek);
    }

    /**
     * 获取当前时间学周
     *
     * @param startDate 学期开始时间
     * @return
     */
    public static int getCurrentSchoolWeek(Date startDate) {
        return getCurrentSchoolWeek(startDate, Calendar.MONDAY);
    }

    /**
     * 获取给定时间星期几,英语国家星期从星期日开始计算,需要减1
     *
     * @param date 给定时间
     * @return
     */
    public static int getWeekDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekday = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (weekday == 0) weekday = 7;
        return weekday;
    }

    /**
     * 获取当前时间星期几
     *
     * @return
     */
    public static int getCurrentWeekDay() {
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        return getWeekDay(date);
    }

    /**
     * 获取上课日期
     *
     * @param startDate      学期开始时间
     * @param week           学周
     * @param firstDayOfWeek 一周开始（从周几开始）
     * @param weekDay        星期几
     * @return
     */
    public static Date getCourseDate(Date startDate, int week, int firstDayOfWeek, int weekDay) {
        Calendar start = Calendar.getInstance();
        start.setFirstDayOfWeek(firstDayOfWeek);
        start.setTime(startDate);
        int amount = (week - 1) * 7 + weekDay - 1;
        start.add(Calendar.DAY_OF_MONTH, amount);
        return start.getTime();
    }

    /**
     * 获取上课日期
     *
     * @param startDate 学期开始时间
     * @param week      学周
     * @param weekDay   星期几
     * @return
     */
    public static Date getCourseDate(Date startDate, int week, int weekDay) {
        return getCourseDate(startDate, week, Calendar.MONDAY, weekDay);
    }
}
